package com.zl.roomdesign.main.mime

import android.app.AlertDialog
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import com.zl.core.MainApp
import com.zl.core.api.ServiceGenerator
import com.zl.core.base.ModeFragment
import com.zl.core.utils.SPUtils
import com.zl.core.view.GridSpacingItemDecoration
import com.zl.roomdesign.R
import com.zl.roomdesign.main.ImageEntity
import com.zl.roomdesign.main.MainApi
import com.zl.roomdesign.main.PreviewActivity
import com.zl.roomdesign.main.RecommendActivity
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.fragment_mine.*

/**
 * Created by zhangli on 2018/5/6,14:18<br/>
 */
class MineFragment : ModeFragment() {

    private val api = ServiceGenerator.createRxService(MainApi::class.java)

    private val list = mutableListOf<ImageEntity>()
    private lateinit var mAdapter: MyCompositionAdapter

    override fun layoutId() = R.layout.fragment_mine

    override fun initView(savedInstanceState: Bundle?) {
        nameTxt.text = MainApp.instance.user!!.name
        phoneTxt.text = MainApp.instance.user!!.phone.toString()

        mAdapter = MyCompositionAdapter(list, {
            val intent = Intent(activity, PreviewActivity::class.java)
            intent.putExtra("imgUrl", MainApp.instance.getServerHost() + list[it.adapterPosition].fileName)
            startActivity(intent)
        })

        recyclerView.layoutManager = GridLayoutManager(context, 3)
        recyclerView.adapter = mAdapter
        recyclerView.addItemDecoration(GridSpacingItemDecoration(3))

        mAdapter.onItemLongClick = {
            AlertDialog.Builder(activity!!)
                    .setMessage("确认删除？")
                    .setPositiveButton("是", { _, _ ->

                        api.delete(list[it.adapterPosition].id!!)
                                .map {
                                    if (it.code == 0) {
                                        return@map it.data
                                    } else {
                                        throw RuntimeException("error" + it.message)
                                    }
                                }
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .doOnTerminate {
                                    loadData()
                                }
                                .subscribe({
                                    showToast("删除成功")
                                }, {
                                    showToast(it.message + "")
                                })
                    })
                    .show()
        }
    }

    override fun setListener() {

        swipeRefreshLayout.setOnRefreshListener {
            loadData()
        }

        headImg.setOnClickListener {
            AlertDialog.Builder(activity)
                    .setMessage("退出")
                    .setPositiveButton("ok", { _, _ ->
                        SPUtils.saveString(SPUtils.PHONE, "")
                        activity?.finish()
                    })
                    .show()
        }

        recommendTxt.setOnClickListener {
            startActivity(Intent(activity, RecommendActivity::class.java))
        }
    }

    override fun observe() {
    }

    override fun afterView() {
        loadData()
    }

    private fun loadData() {
        list.clear()
        api.getByUser(MainApp.instance.user!!.phone)
                .map {
                    if (it.code == 0) {
                        return@map it.data
                    } else {
                        throw RuntimeException("error")
                    }
                }
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnTerminate {
                    swipeRefreshLayout.isRefreshing = false
                    mAdapter.notifyDataSetChanged()
                }
                .subscribe({
                    list.addAll(it!!)
                }, {
                    showToast(it.message + "")
                })
    }
}